using System;

namespace WGT
{
    public partial class Dialog
    {
        protected bool Calendar(pos p, Func<DateTime> get, Action<DateTime> set, string css)
        {
            int id = 0;
            pos op = null;
            string ocss = string.Empty;
            bool rval = false;
            DateTime value = P(get);

            GetPut(ref op, ref p);
            GetPut(ref ocss, ref css);

            if (mode == MODE.SHOW)
            {
                id = iNextId++;

                string templ =
                    "<span id='{0}'><input id='d{0}' onclick=\"javascript:void(0);\" onchange=\"a(w.event(['{0}',this.value]));\" name='d{0}' value='{1}' class='calendar' />" +
                    "<a href='#' onclick=\"javascript:show_calendar_widget(this);\" id='a{0}' class='calendar-link' > ...</a></span>";

                S(templ, id, value.ToShortDateString());
                U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
                U(@"p('d{0}',{1},{2},{3},{4});", id, 0, 0, p.w, p.h);
                U(@"p('a{0}',{1},{2},{3},{4});", id, 0, p.w + 3, p.h, p.h);
                UP(id.ToString(), css);
            }

            GetPut(ref id, ref id);
            string sid = id.ToString();

            if (mode == MODE.UPDATE)
            {
                //if (oldC != caption){
                //    U(@"$('{0}').value={1};", id, caption);
                //}
                if (!p.Equals(op))
                {
                    U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
                }
                //if (attrs != oAttrs)
                //{
                //    UP(sid, attrs);
                //}
            }
            else if (mode == MODE.ERASE)
            {
                E(sid);
            }
            else if (mode == MODE.EVENT)
            {
                if (sender == sid)
                {
                    if (set != null) set(DateTime.Parse(event_args[1]));
                    bUpdate = true;
                    rval = true;
                }
            }

            if (mode != MODE.ERASE)
            {
                if (bHorizontal) gx += p.w;
                else gy += p.h;
            }

            return rval;
        }
    }
}